﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using EBookStore.Users;
using EBookStore.Orders;
using EBookStore.Payments;
using EBookStore.Books;

namespace EBookStore.UnitTests
{

    [TestFixture]
    [Category("objects test")]
    class Test_Order
    {

        Order order;
        
        [SetUp]
        public void init()
        {
            User User = ObjectsGenerator.GenerateCustomers(1).First();
            eOrderVia Via = eOrderVia.WWW;
            Payment payment = ObjectsGenerator.CreateCheckPayment(1)[0];
            order = new Order(User, Via, payment);
        }

        [Test]
        public void AddBooksToOrder()
        {
            Book[] books = (Book[])ObjectsGenerator.GenerateBooks(2,
                (Publishers.Publisher[])ObjectsGenerator.GeneratePublishers(2));                        

            order.AddBook(books[0], 1);
            order.AddBook(books[0], 2);

            // only book amount should be changed
            Assert.AreEqual(1, order.BookList.Count);

            // two different books added
            order.AddBook(books[1], 2);
            Assert.AreEqual(2, order.BookList.Count);
        }


        [Test]
        [ExpectedException(typeof(Exception))]
        public void BookWithNoPrice()
        {
            Book book = ObjectsGenerator.GenerateBooks(1 ,(Publishers.Publisher[])ObjectsGenerator.GeneratePublishers(1)).First();
            book.Price = 0;
            order.AddBook(book, 1);           
        }

        [Test]
        public void CreatedStatus()
        {
            Assert.AreEqual(eOrderStatus.Created, order.HistoryStatus[0].Status);
        }

        [Test]
        public void ChangeStatus()
        {
            order.UpdateOrderStatus(eOrderStatus.Closed);
            Assert.AreEqual(eOrderStatus.Closed, order.Status);            
        }

        [Test]
        [ExpectedException(typeof(Exception),ExpectedMessage="Illegal status!")]
        public void ChangeStatusToPrevious()
        {
            order.UpdateOrderStatus(eOrderStatus.Created);            
        }
        
    }
}
